package cn.edu.sasu.adapter.newsdetail;

import java.io.File;
import java.util.List;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.edu.sasu.R;
import cn.edu.sasu.domain.Comments;
import cn.edu.sasu.http.HttpRequestService;

public class NewsDetailAdapter extends BaseAdapter {
	private List<Comments> list;
	private LayoutInflater inflater;
	private Comments comments;
	private Context context;

	public NewsDetailAdapter(List<Comments> list, Context context) {
		this.context = context;
		this.list = list;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		NewsView nv = null;
		if (convertView == null) {
			nv = new NewsView();
			convertView = inflater.inflate(R.layout.news_content_comment_item,
					null);
			nv.image = (ImageView) convertView
					.findViewById(R.id.iv_topic_image);
			nv.content = (TextView) convertView.findViewById(R.id.tv_content);
			nv.date = (TextView) convertView.findViewById(R.id.tv_date);
			nv.user = (TextView) convertView.findViewById(R.id.tv_user);
			convertView.setTag(nv);
		} else {
			nv = (NewsView) convertView.getTag();
		}
		comments = list.get(position);
		nv.content.setText(comments.getCommentsContent());
		nv.date.setText(comments.getCommentsData());

		nv.user.setText(comments.getUserName()
				+ context.getResources().getString(R.string.nf));
		String photoUrl = context.getResources().getString(R.string.sasufile);
		String photoName = comments.getUserPhoto();
		File file = new File(photoUrl, photoName.split("/")[1]);
		// 从后台读取照片
		if (!file.exists()) {
			new HttpRequestService().putPhoto(photoName, context);
		}
		nv.image.setImageURI(Uri.fromFile(file));
		return convertView;
	}

	private static class NewsView {
		TextView user;
		TextView date;
		ImageView image;
		TextView content;
	}

}
